﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BussinessLayer;

namespace projectQLDV
{
    public partial class frmDoanVienNapDoanPhi : DevExpress.XtraEditors.XtraForm
    {
        private int idDP;
        private frmMain frmM;
        private DataTable dt;
        private DataTable tbdv;
        private int thang;
        private int nam;
        private string namhoc;
        public frmDoanVienNapDoanPhi(frmMain frm, int id, int t, int n)
        {
            InitializeComponent();
            frmM = frm;
            idDP = id;
            //frmM.loadYear(this.comboBoxNamHoc);
            dt = new DataTable();
            thang = t;
            nam = n;
            tbdv = new DataTable();
            this.Text = "Đoàn phí tháng " + thang + " năm " + nam;
            if (thang < 9)
            {
                this.namhoc = (nam - 1).ToString() + "-" + nam.ToString();                
            }
            else
            {
                namhoc = nam.ToString() + "-" + (nam + 1).ToString();
            }
            this.comboBoxChiDoan.Items.Clear();
            tblchidoan cd = new tblchidoan();
            dt.Clear();
            dt = cd.SelectCDTheoNamHoc(namhoc);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow r in dt.Rows)
                {
                    this.comboBoxChiDoan.Items.Add(r["khoi"].ToString() + r["lop"].ToString());
                }
                this.comboBoxChiDoan.SelectedIndex = 0;
            }
        }
        
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void frmDoanVienNapDoanPhi_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmM.frmDVNapDP_isShown = false;
        }

        private void btnDaNap_Click(object sender, EventArgs e)
        {
            try
            {
                tblDoanvien_doanphi dvdp = new tblDoanvien_doanphi();
                foreach (DataRow r in tbdv.Rows)
                {
                    if (bool.Parse(r["select"].ToString()))
                    {
                        if (r["danop"].ToString().Trim() == "Chưa nộp")
                        {
                            dvdp.doanvien_doanphi(int.Parse(r["id"].ToString()), idDP, true);
                        }
                    }
                }
                comboBoxChiDoan_SelectedValueChanged(sender, e);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
        }

        private void btnChuaNap_Click(object sender, EventArgs e)
        {
            try
            {
                tblDoanvien_doanphi dvdp = new tblDoanvien_doanphi();
                foreach (DataRow r in tbdv.Rows)
                {
                    if (bool.Parse(r["select"].ToString()))
                    {
                        if (r["danop"].ToString().Trim() == "Đã nộp")
                        {
                            dvdp.doanvien_doanphi(int.Parse(r["id"].ToString()), idDP, false);
                        }
                    }
                }
                comboBoxChiDoan_SelectedValueChanged(sender, e);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
        }

        private void gridViewDV_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {

        }

        #region Chọn chi đoàn
        private void comboBoxChiDoan_SelectedValueChanged(object sender, EventArgs e)
        {
            int _chidoanID = int.Parse(dt.Rows[this.comboBoxChiDoan.SelectedIndex]["chidoan_id"].ToString());
            tbldoanphi dp = new tbldoanphi();
            tblDoanvien dv = new tblDoanvien();
            tbdv.Clear();
            DataTable tbdp = new DataTable();
            try
            {
                tbdv = dv.SelectDVThuocChiDoan(_chidoanID);
                tbdp = dp.SelectDVDaNapDP(idDP);
                tbdv.Columns.Add("danop",typeof(string));
                tbdv.Columns.Add("select",typeof(bool));
                tbdv.Columns.Add("gt", typeof(string));
                foreach (DataRow r in tbdv.Rows)
                {
                    r["select"] = false;
                    r["danop"] = "Chưa nộp";
                    if (bool.Parse(r["gioitinh"].ToString()))
                        r["gt"] = "Nam";
                    else r["gt"] = "Nữ";
                    for (int i = 0; i < tbdp.Rows.Count; i++)
                    {
                        if (r["id"].ToString() == tbdp.Rows[i]["id"].ToString())
                        {
                            r["danop"] = "Đã nộp";
                            break;
                        }
                    }
                }
                this.gridControlDV.DataSource = tbdv;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
        }
        #endregion

        private void repositoryItemCheckEdit_CheckedChanged(object sender, EventArgs e)
        {
            
        }
    }
}